package com.example.kehoobackend.controller;


import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Validated
@CrossOrigin
@RestController
public class FollowController {

    @Autowired
    private FollowService followService;


    /**
     * 获取用户关注列表
     *
     * @param user_id 用户id
     * @param type 关注对象类型 0-用户 1-问题 2-话题
     * @return CommonResponse<Map<String, Object>>
     */
    @GetMapping("/follow")
    public CommonResponse<Map<String, Object>> allFollow(
            @RequestParam("user_id") int user_id,
            @RequestParam("type") int type) {
        return CommonResponse.createForSuccess("关注列表", followService.allFollow(user_id, type));
    }

    /**
     * 获取用户关注数
     *
     * @param user_id 用户id
     * @param type 关注对象类型 0-用户 1-问题 2-话题
     * @return CommonResponse<Integer>
     */
    @GetMapping("/follow/count")
    public CommonResponse<Integer> countUserFollow(
            @RequestParam("user_id") int user_id,
            @RequestParam("type") int type) {
        return CommonResponse.createForSuccess("关注数", followService.countUserFollow(user_id, type));
    }

    /**
     * 获取用户粉丝数
     *
     * @param id 用户id
     * @param type 关注对象类型 0-用户 1-问题 2-话题
     * @return CommonResponse<Integer>
     */
    @GetMapping("/follow/fans/count")
    public CommonResponse<Integer> countFans(
            @RequestParam("id") int id,
            @RequestParam("type") int type) {
        return CommonResponse.createForSuccess("粉丝数", followService.countFans(id, type));
    }

    /**
     * 查询用户是否关注
     *
     * @param from_id 用户id
     * @param to_id 关注对象id
     * @param type 关注对象类型 0-用户 1-问题 2-话题
     * @return CommonResponse<Boolean>
     */
    @GetMapping("/follow/isFollow")
    public CommonResponse<Boolean> isFollow(
            @RequestParam("from_id") int from_id,
            @RequestParam("to_id") int to_id,
            @RequestParam("type") int type) {
        return followService.isFollow(from_id, to_id, type)
                ? CommonResponse.createForSuccess("未关注",false)
                : CommonResponse.createForSuccess("已关注",true);
    }

    /**
     * 获取用户粉丝列表
     *
     * @param id 用户id
     * @return CommonResponse<List<User>>
     */
    @GetMapping("/follow/fans")
    public CommonResponse<List<User>> allFans(
            @RequestParam("id") int id) {
        return CommonResponse.createForSuccess("粉丝列表", followService.allFans(id, 0));
    }




    /**
     * 用户关注
     *
     * @param from_id 用户id
     * @param to_id 关注对象id
     * @param type 关注对象类型 0-用户 1-问题 2-话题
     * @return CommonResponse<Boolean>
     */
    @PostMapping("/follow")
    public CommonResponse<Boolean> followOne(
            @RequestParam("from_id") int from_id,
            @RequestParam("to_id") int to_id,
            @RequestParam("type") int type) {
        return followService.followOne(from_id, to_id, type)
                ? CommonResponse.createForSuccess("关注成功",true)
                : CommonResponse.createForSuccess("关注失败",false);
    }



    /**
     * 用户取消关注
     *
     * @param from_id 用户id
     * @param to_id 关注对象id
     * @param type 关注对象类型 0-用户 1-问题 2-话题
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/follow")
    public CommonResponse<Boolean> cancelOne(
            @RequestParam("from_id") int from_id,
            @RequestParam("to_id") int to_id,
            @RequestParam("type") int type) {
        return followService.cancelOne(from_id,to_id,type)
                ? CommonResponse.createForSuccess("取消关注成功",true)
                : CommonResponse.createForSuccess("取消关注失败",false);
    }

}
